package com.twu.biblioteca;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Scanner;

public class Biblioteca {
    private HashMap<Integer,String> memberMenu;
    private HashMap<Integer,String> publicMenu;
    private User currentUser;
    private ArrayList<Book> bookList;
    private ArrayList<Movie> movieList;
    private ArrayList<User> userList;

    // Private Constants
    private static final String WELCOME_MESSAGE = "" +
            "\n                          ????.            ..DDDDD        " +
            "\n                          ???????.       .DDDDDDDD        " +
            "\n                     .DD8 ?????????.    ?DDDDDDDDD.++?    " +
            "\n                      DDD ??????????.  DDDDDDDDDDD.???    " +
            "\n                      DDD ??????????? .DDDDDDDDDDD.???    " +
            "\n                  ~:. DDD ??????????? =DDDDDDDDDDD.??? .. " +
            "\n                  +?. DDD ??????????? =DDDDDDDDDDD.??? DD " +
            "\n                  +?. DDD ??????????? =DDDDDDDDDDD.??? DD " +
            "\n                  +?. DDD ??????????? =DDDDDDDDDDD.??? DD " +
            "\n                  ??. DDD ??????????? =DDDDDDDDDDD.??? DD " +
            "\n                  +?. DDD ??????????? =DDDDDDDDDDD.??? DD " +
            "\n                  +?. DDD.??????????? =DDDDDDDDDDD.??? DD " +
            "\n                  +?. DDDDD.. ??????? =DDDDDD...?????? DD " +
            "\n                  +?. DDDDDDDD: .???? =DDDI .????????? DD " +
            "\n                  +?. DDDDDDDDDDD ??? =DD..??????????? DD " +
            "\n                  +?.   ...8DDDDDD..? =D.???????. .    DD " +
            "\n                  +????????+.  ..DD?    ???.  .,DDDDDDDDD " +
            "\n                  +??????????????..D   ?. 7DDDDDDDDDDDDDD " +
            "\n                  ..................    ................." +
            "\n                           ,,  ,,        ,,    ,," +
            "\n`7MM\"\"\"Yp,   db *MM      `7MM    db           mm                   " +
            "\n  MM    Yb       MM        MM                 MM                   " +
            "\n  MM    dP `7MM  MM,dMMb.  MM  `7MM  ,pW\"Wq.mmMMmm .gP\"Ya   ,p6\"bo   ,6\"Yb." +
            "\n  MM\"\"\"bg.   MM  MM    `Mb MM    MM 6W'   `Wb MM  ,M'   Yb 6M'  OO  8)   MM" +
            "\n  MM    `Y   MM  MM     M8 MM    MM 8M     M8 MM  8M\"\"\"\"\"\" 8M        ,pm9MM" +
            "\n  MM    ,9   MM  MM.   ,M9 MM    MM YA.   ,A9 MM  YM.    , YM.    , 8M   MM" +
            "\n.JMMmmmd9  .JMML.P^YbmdP'.JMML..JMML.`Ybmd9'  `Mbmo`Mbmmd'  YMbmd'  `Moo9^Yo." +
            "\n-----------------------------------------------------------------------------" +
            "\n--------------- Welcome to the Biblioteca Library Application ---------------" +
            "\n-----------------------------------------------------------------------------\n";
    private static final String DISPLAY_ALL_BOOKS_SECTION_HEADER = "" +
            "\n#####################" +
            "\n# Display All Books #" +
            "\n#####################";
    private static final String DISPLAY_ALL_MOVIES_SECTION_HEADER = "" +
            "\n######################" +
            "\n# Display All Movies #" +
            "\n######################";

    // Public constants
    public final String MENU_SELECTION_PROMPT = "Please select an option: ";
    public final String BOOK_ID_PROMPT = "Please enter a book ID: ";
    public final String REQUEST_USERNAME_PROMPT = "Please enter username (Type 'NO' to continue without logging in): ";
    public final String REQUEST_PASSWORD_PROMPT = "Please enter password: ";

    public Biblioteca() {
        memberMenu = new HashMap<Integer, String>();
        publicMenu = new HashMap<Integer, String>();
        bookList = new ArrayList<Book>();
        movieList = new ArrayList<Movie>();
        userList = new ArrayList<User>();
    }

    public void createDummyData() {
        createMenu();
        createBookList();
        createMovieList();
        createDummyUserList();
    }

    public String getWelcomeMessage() {
        return WELCOME_MESSAGE;
    }

    public HashMap<Integer,String> getMenu(boolean userLoggedIn) {
        if (userLoggedIn) return memberMenu;

        return publicMenu;
    }

    public String requestUserInput(String message) {
        Scanner scanner = new Scanner(System.in);
        System.out.print(message);
        return scanner.next();
    }


    public String getDisplayAllBooksSectionHeader() {
        return DISPLAY_ALL_BOOKS_SECTION_HEADER;
    }

    public String getBookListRecordHeadings(String format) {
        return String.format(format, "ID", "Title", "Author", "Available?");
    }

    public ArrayList<Book> getBookList() {
        return bookList;
    }

    public boolean reserveBook(int bookId) {
        for (Book aBook : bookList) {
            if (aBook.getId() == bookId) {
                if (!aBook.isReserved()) {
                    aBook.reserveBook();
                    return true;
                }
            }
        }

        return false;
    }

    public ArrayList<Movie> getMovieList() {
        return movieList;
    }

    public String getDisplayAllMoviesSectionHeader() {
        return DISPLAY_ALL_MOVIES_SECTION_HEADER;
    }

    public String getMovieListRecordHeadings(String format) {
        return String.format(format, "Movie", "Year", "Director", "Rating");
    }

    public ArrayList<User> getUserList() {
        return userList;
    }

    public void addUserToUserList(User user) {
        userList.add(user);
    }

    public boolean verifyUserLoginDetails(String username, String password) {
        for (User user : userList) {
            if (user.getUsername().equals(username) && user.verifyPassword(password))  return true;
        }

        return false;
    }

    public User getCurrentUser() {
        return currentUser;
    }

    public void setCurrentUser(String username) {
        for (User user : userList) {
            if (user.getUsername().equals(username)) {
                currentUser = user;
            }
        }
    }

    private void createMenu() {
        memberMenu.put(1, "Display all Books");
        memberMenu.put(2, "Display all Movies");
        memberMenu.put(3, "Reserve Book for Collection");
        memberMenu.put(4, "Check Membership Details");
        memberMenu.put(5, "Quit Application");

        publicMenu.put(1, "Display all Books");
        publicMenu.put(2, "Display all Movies");
        publicMenu.put(3, "Check Membership Details");
        publicMenu.put(4, "Quit Application");


    }

    /**
     * Create the required books and add them to the book list.
     */
    private void createBookList() {
        Book book;

        book = new Book(1,"Head First Java", "Kathy Sierra");
        bookList.add(book);

        book = new Book(2, "The Pragmatic Programmer", "Andrew Hunt");
        bookList.add(book);

        book = new Book(3, "The Agile Samurai", "Jonathon Rasmusson");
        bookList.add(book);
    }

    /**
     * Create the required movies and add them to the movie list.
     */
    private void createMovieList() {
        Movie movie;

        movie = new Movie("Batman Begins", 2005, "Christopher Nolan");
        movie.setRating(10);
        movieList.add(movie);

        movie = new Movie("The Dark Knight", 2008, "Christopher Nolan");
        movie.setRating(9);
        movieList.add(movie);

        movie = new Movie("The Dark Knight Rises", 2012, "Christopher Nolan");
        movie.setRating(10);
        movieList.add(movie);

        movie = new Movie("Inception", 2010, "Christopher Nolan");
        movie.setRating(8);
        movieList.add(movie);

        movie = new Movie("Man of Steel", 2013, "Zack Snyder");
        movieList.add(movie);

        movie = new Movie("John Carter", 2012, "Andrew Stanton");
        movie.setRating(7);
        movieList.add(movie);

        movie = new Movie("The Phantom Menace", 1999, "George Lucas");
        movie.setRating(5);
        movieList.add(movie);

        movie = new Movie("Avatar", 2009, "James Cameron");
        movie.setRating(9);
        movieList.add(movie);

        movie = new Movie("Iron Man 3", 2013, "Shane Black");
        movieList.add(movie);

        movie = new Movie("The Great Gatsby", 2013, "Baz Luhrmann");
        movieList.add(movie);

        movie = new Movie("Django", 2012, "Quentin Tarantino");
        movie.setRating(8);
        movieList.add(movie);

        movie = new Movie("Hellboy", 2004, "Guillermo Del Toro");
        movie.setRating(4);
        movieList.add(movie);

        movie = new Movie("The Mummy Returns", 2001, "Stephen Sommers");
        movie.setRating(3);
        movieList.add(movie);

        movie = new Movie("Beowulf", 2007, "Robert Zemeckis");
        movie.setRating(9);
        movieList.add(movie);

        movie = new Movie("The Incredibles", 2004, "Brad Bird");
        movie.setRating(6);
        movieList.add(movie);
    }

    /**
     * Create the required users and add them to the user list.
     */
    private void createDummyUserList() {
        User user = new User();

        user.setPassword("password");
        user.setName("Anthony Sceresini");
        user.setEmail("anthony@email.com");
        user.setPhone("+61403000001");

        addUserToUserList(user);
    }
}
